package com.server.server.handler.websocket;

import com.server.exception.BusinessException;
import com.server.model.enums.ExceptionInfo;
import com.server.rule.engine.RuleBootStrap;
import com.server.rule.protocolInfo.Message;
import com.server.rule.protocolInfo.ProtocolHeader;
import com.server.rule.protocolInfo.ProtocolInfo;
import com.server.util.MessageParserUtil;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.socket.DatagramPacket;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.handler.codec.http.websocketx.WebSocketFrame;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import lombok.extern.slf4j.Slf4j;

import java.net.DatagramSocket;

@Slf4j
public class WebSocketHandler extends SimpleChannelInboundHandler<WebSocketFrame> {
    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        if (evt instanceof WebSocketServerProtocolHandler.HandshakeComplete) {
            WebSocketServerProtocolHandler.HandshakeComplete handshake = (WebSocketServerProtocolHandler.HandshakeComplete) evt;
            // 获取WebSocket连接的子协议
            String subprotocol = handshake.requestHeaders().get("Sec-WebSocket-Protocol");
            System.out.println("WebSocket子协议: " + subprotocol);

            // 解析子协议中的参数，这部分逻辑根据子协议的格式进行自定义
            // 例如，如果子协议是 "your-subprotocol:param1=value1,param2=value2"，
            // 您可以解析参数并处理它们。
        }
    }

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, WebSocketFrame frame) throws Exception {
        // 如果接受文本信息
        if (frame instanceof TextWebSocketFrame) {
            // 接收前端发送的文本消息
            String message = ((TextWebSocketFrame) frame).text();
            log.info(message);
            // 使用正则表达式提取摄像头编号、操作和协议信息
            String cameraId = MessageParserUtil.extractCameraId(message);
            String operation = MessageParserUtil.extractOperation(message);
            String protocol = MessageParserUtil.extractProtocol(message);
            // 校验打印值
            if(operation==null){
                throw new BusinessException(ExceptionInfo.PARAMS_ERROR,"操作失败，未接收该操作参数!");
            }
            if(cameraId==null){
                throw new BusinessException(ExceptionInfo.PARAMS_ERROR,"操作失败，未接受摄像头Id!");
            }
            log.info("cameraId:"+cameraId);
            log.info("operation:"+operation);
            log.info("protocol:"+protocol);
            // 封装信息
            Message<String> stringMessage = new Message<>();
            stringMessage.setData("HttpReport111");
            ProtocolHeader protocolHeader = new ProtocolHeader();
            protocolHeader.setDestinationIP("127.0.0.1");
            protocolHeader.setDestinationPort("8091");
            protocolHeader.setProtocolInfo(new ProtocolInfo("HTTP","HTTP"));
            stringMessage.setProtocolHeader(protocolHeader);
            log.info("规则引擎模块匹配");

            log.info("规则引擎结束匹配");
        }
    }
}
